package test;

import java.util.HashSet;
import java.util.Set;

/*
36. 有效的数独
判断一个 9x9 的数独是否有效。只需要根据以下规则，验证已经填入的数字是否有效即可。

数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。

数据
输入:
[
  ["5","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6"],
  [".","6",".",".",".",".","2","8","."],
  [".",".",".","4","1","9",".",".","5"],
  [".",".",".",".","8",".",".","7","9"]
]
输出: true
输入:
[
  ["8","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6"],
  [".","6",".",".",".",".","2","8","."],
  [".",".",".","4","1","9",".",".","5"],
  [".",".",".",".","8",".",".","7","9"]
]
输出: false
解释: 除了第一行的第一个数字从 5 改为 8 以外，空格内其他数字均与 示例1 相同。
     但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
 */
public class leetcode36_youxiaoshuzu {
    //方法一，使用hashset进行排序
    public static boolean isValidSudoku(char a[][]){
        Set<Character>[] rows=new Set[9];      //统计行set
        Set<Character>[] cols=new Set[9];       //统计列set
        Set<Character>[] boxs=new Set[9];       //统计九宫格set
        for(int i=0;i<9;i++){
            rows[i]=new HashSet<>();
            cols[i]=new HashSet<>();
            boxs[i]=new HashSet<>();
        }
        for (int row = 0; row < 9; row++) {
            for (int col = 0; col < 9; col++) {
                char num = a[row][col];
                if (num == '.') {
                    continue;
                }

                System.out.println(1111);

                if (rows[row].contains(num)) return false;                //比较行或者列时，只需要比较前面部分
                if (cols[col].contains(num)) return false;
                if(boxs[(row/3)*3+col/3].contains(num)) return false;
                rows[row].add(num);                 //将num加入到set里面保存
                cols[col].add(num);
                boxs[(row/3)*3+col/3].add(num);
            }
        }
        return true;
    }
    //方法二 采用数组形式,记录下标来实现
    public boolean isValidSudoku1(char[][] board) {
        int [][]row  =new int[9][10];
        int [][]col  =new int[9][10];
        int [][]box  =new int[9][10];
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (board[i][j]=='.'){
                    continue;
                }
                int curNum = board[i][j]-'0';
                if (row[i][curNum]==1){    //用于判断
                    return false;
                }if (col[j][curNum]==1){
                    return false;
                }
                if (box[j/3 + (i/3) * 3][curNum]==1){
                    return false;
                }
                row[i][curNum]=1;    //如果下一次还会遍历同样的改下标就退出
                col[j][curNum]=1;
                box[j/3 + (i/3) * 3][curNum]=1;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        char a[][]={ {'8','3','.','.','7','.','.','.','.'},
                {'6','.','.','1','9','5','.','.','.'},
                {'.','9','8','.','.','.','.','6','.'},
                {'.','9','8','.','.','.','.','6','.'},
                {'.','9','8','.','.','.','.','6','.'},
                {'.','9','8','.','.','.','.','6','.'},
                {'.','9','8','.','.','.','.','6','.'},
                {'.','9','8','.','.','.','.','6','.'},
                {'.','9','8','.','.','.','.','6','.'}};
        System.out.println(isValidSudoku(a));

    }
}
